package com.zhuchaooasystem.utils;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.HashMap;


/**
 * @author yinsi
 *         <p/>
 *         加密工具类
 */
public class EncryptUtils {

    /**
     * MD5加密，32位
     *
     * @param str
     * @return
     */
    public static String MD5(String str) {
        try {
            byte[] btInput = str.getBytes("GBK");
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < md.length; i++) {
                int val = ((int) md[i]) & 0xff;
                if (val < 16) {
                    sb.append("0");
                }
                sb.append(Integer.toHexString(val));
            }
            return sb.toString();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 用户密码加密
     *
     * @param password
     * @return
     */
    public static String encryptPassword(String password) {
        String newPassword = "";
        try {
            String passwordCrop = password.substring(0, 6);
            String randomLetter = RandomUtils.getRandomLetter(7);
            String passwordConcat = String.valueOf(randomLetter.charAt(0));
            for (int i = 0; i < passwordCrop.length(); i++) {
                passwordConcat += String.valueOf(passwordCrop.charAt(i));
                passwordConcat += String.valueOf(randomLetter.charAt(i + 1));
            }
            if (password.length() > 6) {
                passwordConcat += password.substring(6);
            }
            String passwordReverse = String.valueOf(TextUtils.getReverse(passwordConcat, 0, passwordConcat.length()));
            newPassword = getUTFTo64(passwordReverse);
        } catch (Exception e) {
        }
        return newPassword;
    }


    /**
     * BASE64 加密
     *
     * @param str
     * @return
     */
    public static String getUTFTo64(String str) {
        try {
            byte[] encode = str.getBytes("UTF-8");
            // base64 加密
            return new String(Base64.encode(encode, 0, encode.length, Base64.DEFAULT), "UTF-8");
        } catch (UnsupportedEncodingException e) {
        }
        return null;
    }

    public static String getuniqueId(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String imei = tm.getDeviceId();
        return imei;
    }

    public static String getSign(Context context,String noce_str) {
        HashMap<String, String> map_sign = new HashMap<>();
        map_sign.put("version", AppUtils.getAppVersionName(context));
        map_sign.put("systemNum", "android");
        map_sign.put("nonce_str", noce_str);
        String string_map = SortUtils.sort(map_sign);
        return MD5(string_map + "&key=" + "testinterface").toUpperCase();
    }

}